//
//  LoginVC.swift
//  Employee
//
//  Created by swordjoy on 2017/3/14.
//  Copyright © 2017年 remotevillage. All rights reserved.
//

import UIKit
import SnapKit
import IQKeyboardManager

class LoginVC: SJBaseHiddenVC {
  
  fileprivate var model = LoginModel()
  
  fileprivate var contentV: UIView!
  
  fileprivate var titleLabel: UILabel!
  
  fileprivate var usernameTF: UITextField!
  fileprivate var passwordTF: UITextField!
  fileprivate var loginButton: UIButton!
  fileprivate var forgetButton: UIButton!
  
    override var preferredStatusBarStyle: UIStatusBarStyle {
        return .default
    }
    
  override func viewDidLoad() {
    super.viewDidLoad()
    
    view.backgroundColor = GlobalConfig.BGWhiteColor
    
    create()
    gesture()
  }
  
  override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    
    usernameTF.text = Account.userAccount
    passwordTF.text = ""
    navigationController?.setNavigationBarHidden(true, animated: true)
    
    
    GlobalConfig.baiduStatisticsPageViewStart("登录")
  }
  
  override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)
    navigationController?.setNavigationBarHidden(false, animated: true)
    
    
    GlobalConfig.baiduStatisticsPageViewEnd("登录")
  }
  
  deinit {
//    SJApp.log("Login deinit")
  }
  
}

// MARK: - action

extension LoginVC {
  
  @objc fileprivate func loginAction() {
    endEditAction()
    
    model.username = usernameTF.text!
    model.password = passwordTF.text!
    
    SJApp.showHUD()
    // 登录
    let _ = UserRequest.shared.login(with: model, loginSuccess: {
        SJApp.hideHUD()
        runOnMainThread {
            self.navigationController?.dismiss(animated: true, completion: nil)
        }
        // 这里登录成功先缓存一遍通讯录
        runInBackground {
            let _ = UserRequest.shared.getUserAddressBook(success: { (chatUsers) in
                SJCache.chatUsers = chatUsers
            })
        }
    }) { 
        SJApp.hideHUD()
    }

  }
  
  @objc fileprivate func endEditAction() {
    view.endEditing(true)
  }
  
  @objc fileprivate func forgetAction() {
    SJApp.endEdit()
    let vc = VerifyVC()
    navigationController?.pushViewController(vc, animated: true)
  }
  
}

// MARK: - fileprivate

extension LoginVC {
  
  fileprivate func gesture() {
    
    let tap = UITapGestureRecognizer(target: self, action: #selector(endEditAction))
    contentV.addGestureRecognizer(tap)
    
  }
  
  fileprivate func create() {
    
    contentV = UIView(frame: UIScreen.main.bounds)
    contentV.backgroundColor = GlobalConfig.BGWhiteColor
    view.addSubview(contentV)
    
    titleLabel = UILabel()
    contentV.addSubview(titleLabel)
    titleLabel.font = GlobalConfig.bold20Font
    titleLabel.textColor = GlobalConfig.TextDeepColor
    titleLabel.text = "登录账号"
    titleLabel.snp.makeConstraints { (make) in
      make.centerX.equalToSuperview()
      make.top.equalTo(60)
    }
    
    usernameTF = UITextField()
    contentV.addSubview(usernameTF)
    usernameTF.placeholder = "手机号"
    usernameTF.leftView = UIView(frame: CGRect(x: 0, y: 0, width: 15, height: 50))
    usernameTF.leftViewMode = .always
    usernameTF.clearButtonMode = .whileEditing
    usernameTF.backgroundColor = UIColor.white
    usernameTF.keyboardType = .numberPad
    usernameTF.snp.makeConstraints { (make) in
      make.left.equalTo(10)
      make.right.equalTo(-10)
      make.height.equalTo(50)
      make.top.equalTo(titleLabel.snp.bottom).offset(60)
    }
    usernameTF.layer.cornerRadius = 4
    usernameTF.clipsToBounds = true
    
    passwordTF = UITextField()
    contentV.addSubview(passwordTF)
    passwordTF.placeholder = "密码"
    passwordTF.leftView = UIView(frame: CGRect(x: 0, y: 0, width: 15, height: 50))
    passwordTF.leftViewMode = .always
    passwordTF.clearButtonMode = .whileEditing
    passwordTF.backgroundColor = UIColor.white
    passwordTF.isSecureTextEntry = true
    passwordTF.snp.makeConstraints { (make) in
      make.left.equalTo(10)
      make.right.equalTo(-10)
      make.height.equalTo(50)
      make.top.equalTo(usernameTF.snp.bottom).offset(10)
    }
    passwordTF.layer.cornerRadius = 4
    passwordTF.clipsToBounds = true
    
    loginButton = UIButton()
    contentV.addSubview(loginButton)
    loginButton.setTitle("登录", for: .normal)
    loginButton.backgroundColor = GlobalConfig.BGSkyBlueColor
    loginButton.setTitleColor(UIColor.white, for: .normal)
    loginButton.titleLabel?.font = GlobalConfig.system15Font
    loginButton.addTarget(self, action: #selector(loginAction), for: .touchUpInside)
    loginButton.snp.makeConstraints { (make) in
      make.left.equalTo(10)
      make.right.equalTo(-10)
      make.height.equalTo(50)
      make.top.equalTo(passwordTF.snp.bottom).offset(20)
    }
    loginButton.layer.cornerRadius = 4
    loginButton.clipsToBounds = true
    
    forgetButton = UIButton()
    contentV.addSubview(forgetButton)
    forgetButton.setTitle("忘记密码?", for: .normal)
    forgetButton.setTitleColor(GlobalConfig.BGSkyBlueColor, for: .normal)
    forgetButton.titleLabel?.font = GlobalConfig.system15Font
    forgetButton.addTarget(self, action: #selector(forgetAction), for: .touchUpInside)
    forgetButton.snp.makeConstraints { (make) in
      
      make.right.equalTo(-18)
      make.top.equalTo(loginButton.snp.bottom).offset(28)
      make.width.equalTo(100)
      make.height.equalTo(20)
      
    }
  }
}
